package org.budo.dubbo.rpc.util;

import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;

/**
 * @author lmw
 */
public class InvocationUtil {
    /**
     * TODO 每次都执行？
     */
    public static String invocationToRequestUri(Invocation invocation) {
        Invoker<?> invoker = invocation.getInvoker();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(invoker.getInterface().getName());
        stringBuilder.append(".");
        stringBuilder.append(invocation.getMethodName());
        stringBuilder.append("(");
        Class<?>[] parameterTypes = invocation.getParameterTypes();
        for (int i = 0; i < parameterTypes.length; i++) {
            if (i > 0) {
                stringBuilder.append(",");
            }

            Class<?> parameterType = parameterTypes[i];
            if (parameterType.isArray()) {
                Class<?> componentType = parameterType.getComponentType();
                String canonicalName = componentType.getCanonicalName();
                stringBuilder.append(canonicalName).append("...");
            } else {
                String parameterTypeName = parameterType.getName();
                stringBuilder.append(parameterTypeName);
            }
        }

        stringBuilder.append(")");
        return stringBuilder.toString();
    }
}